import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/request")
public class ShowRequestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("<h3>首行部分</h3>");
        stringBuilder.append(req.getProtocol());// 得到协议版本
        stringBuilder.append("<br>");

        stringBuilder.append(req.getMethod());// 请求的方法
        stringBuilder.append("<br>");

        stringBuilder.append(req.getRequestURI());// 请求的 URL
        stringBuilder.append("<br>");

        stringBuilder.append(req.getContextPath());// 上下文的路径
        stringBuilder.append("<br>");

        stringBuilder.append(req.getQueryString());// 请求参数
        stringBuilder.append("<br>");

        stringBuilder.append("<h3>header部分</h3>");
        Enumeration<String> names = req.getHeaderNames();// 得到 header中所有的 key值
        while (names.hasMoreElements()){
            String name = names.nextElement();// 得到 单个 key值
            String value = req.getHeader(name);// 根据 key得到value
            stringBuilder.append(name);
            stringBuilder.append(" : ");
            stringBuilder.append(value);
            stringBuilder.append("<br>");
        }

        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write(stringBuilder.toString());
    }
}















